package com.atguigu.ssyx.acl.service.Impl;

import com.atguigu.ssyx.acl.mapper.RolePermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private PermissionService permissionService;


    @Override
    public Map<String, Object> getPermissionList(List<RolePermission> list) {
        //        获得权限的id
        List<Long> PermissionIds = list.stream().map(c -> c.getPermissionId()).collect(Collectors.toList());
//        根据权限id获得整个权限的信息.
        List<Permission> listAll = permissionService.list();
//        找到目标list中所有的权限id的对象了.
        List<Permission> collect = listAll.stream().filter(item -> PermissionIds.contains(item.getId())).collect(Collectors.toList());

//      本来是要两个for的;但是集合的contains方法已经做了优化;
//filter(()->{
//       目标list.contains(单个list元素)
// })


        Map<String, Object> map = new HashMap<>();
        map.put("assignRoles", collect);
        map.put("permissionList", listAll);
        return map;
    }
}
